{layout name="common/base" }

<div class="page-container">
	<form class="form form-horizontal" id="cms-form">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">名称：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="<?php echo $data['name'];?>" placeholder="" id="name" name="name">
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">上级分类：</label>
			<div class="formControls col-xs-8 col-sm-9">
<select name="pid">
	<option value="0">顶级分类</option>
	<?php foreach ($parentData as $k => $v): ?>
	<?php if($v['id'] == $data['id'] || in_array($v['id'], $children)) continue ; ?>
	<option <?php if($v['id'] == $data['pid']): ?>selected="selected" <?php endif; ?> value="<?php echo $v['id']; ?>"><?php echo str_repeat('-', 2*$v['level']).$v['name']; ?></option>
	<?php endforeach; ?>
</select>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>上传图片：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<img style="display: block" id="upload_image" src="<?php echo $data['thumb'];?>" width="150" height="150">
				<input id="file_upload"  type="file" multiple="true" >
				<input id="file_upload_image" name="thumb" type="hidden" multiple="true" value="<?php echo $data['thumb'];?>">
				<div>(尺寸150x150像素以上，大小2M以下)</div>
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">排序值：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="<?php echo $data['sort'];?>" placeholder="" id="sort" name="sort">(数字越小越靠前)
			</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
				<button class="btn btn-primary radius" type="button" id="cms-btn-submit"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
				<button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
			</div>
		</div>
	</form>
</div>

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__PUBLIC__/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__PUBLIC__/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__PUBLIC__/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="__STATIC__/uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript">
    $(function(){

        $("#file_upload").uploadify({
            swf             : '/static/uploadify/uploadify.swf',
            uploader        : '/common/image/upload',
            buttonText      : '上传图片',
            fileTypeDesc    : 'Image files',
            fileSizeLimit	  : '2MB',
            uploadLimit     : 1,
            fileObjName     : 'file',
            fileTypeExts    : '*.gif; *.jpg; *.png',
            onUploadSuccess : function(file, data, response) {
                if(response) {
                    var obj = JSON.parse(data);
                    $("#upload_image").attr("src", obj.data.path);
                    $("#file_upload_image").attr("value", obj.data.path);
                    $("#upload_image").show();
                }
            },
            onUploadStart: function (file) {
                if(file.size > 2097152){ //2*1024*1024
                    alert("图片不能大于2M");
                    $('#upload').uploadify('cancel'); //注意写法
                }
                var swfu = $('#file_upload').data('uploadify');
                var stats = swfu.getStats();
                stats["successful_uploads"]--;
                swfu.setStats(stats);
            },
        });

        $("#cms-btn-submit").click(function(){
            var data = $("#cms-form").serializeArray();
            postData = {};
            $(data).each(function(i){
                postData[this.name] = this.value;
            });
            // 将获取到的数据post给服务器
            $.post('{:url("admin/category/edit")}',postData,function(data){
                if(data.code == 1){
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000,
                        end: function () {
                            window.parent.location.reload();//刷新父窗口
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            //parent.$('#btn-refresh').click();
                        }
                    });
                }
            },"JSON");
        });

    });

</script>
